<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="任务名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="sumaryHandle()" :disabled="dataListSelections.length <= 0">统计汇总</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="taskName"
        header-align="center"
        align="center"
        label="任务名称"
        width="200">
      </el-table-column>
      <el-table-column
        prop="tempName"
        header-align="center"
        align="center"
        label="模板"
        width="200">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="上报人姓名">
      </el-table-column> 
      <el-table-column
        prop="reportTime"
        header-align="center"
        align="center"
        label="上报时间"
        width="200"
        >
      </el-table-column>
      <el-table-column
        prop="nodeDate"
        header-align="center"
        align="center"
        label="上报时间节点">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        v-if="!(this.$cookie.get('level') == 4)"
        label="上报数据"> 
        <template slot-scope="scope">
          <a v-if="scope.row.id" :href="serverFileUrl + '/frt/taskuser/download?flag=manager&tempType='+scope.row.tempType+'&taskUserId='+scope.row.id+'&token='+token"
            target="_blank"
            class="buttonText">下载数据</a>
        </template> 
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import global_ from '@/components/tool/Global'

import AddOrUpdate from './taskuser-sumary-update'

  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: ''
        },
        serverFileUrl: global_.SERVER_FILE_URL,
        token: this.$cookie.get('token'),
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
      }
    },
    activated () {
      this.getDataList()
    },
    components: {
      AddOrUpdate
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/frt/taskuser/next'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'flag': 'sumary'
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

      // 统计汇总
      sumaryHandle (id){
        var vaildTask = true;
        var vaildTemp = true;
        var taskName = '';
        var tempName = '';
        this.dataListSelections.map(item => {
          if(taskName !== ''){
            if(item.taskName !== taskName){
                vaildTask = false;
                return;
            }
          }else{
            taskName = item.taskName; 
          }

          if(tempName !== ''){
            if(item.tempName !== tempName){
                vaildTemp = false;
                return;
            }
          }else{
            tempName = item.tempName; 
          }
        })

        if(!vaildTemp){
          this.$message.error('请相同模板类型的数据！')
          return false;
        }

        if(!vaildTask){
          this.$message.error('请相同任务的数据！')
          return false;
        }
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '统计汇总' : '批量统计汇总'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.addOrUpdateVisible = true
          this.$nextTick(() => {
            this.$refs.addOrUpdate.init(ids)
          })
        })
      }
    }
  }
  
</script>
